package com.java110.mall.openapi.bmo.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.dto.gold.GoldDetailDto;
import com.java110.intf.acct.IGoldDetailV1InnerServiceSMO;
import com.java110.mall.openapi.bmo.IMallCommonApiBmo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("queryPropertyGoldDetailBmoImpl")
public class QueryPropertyGoldDetailBmoImpl implements IMallCommonApiBmo {

    @Autowired
    private IGoldDetailV1InnerServiceSMO goldDetailV1InnerServiceSMOImpl;

    @Override
    public void validate(ICmdDataFlowContext context, JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "communityId", "未包含小区");
        Assert.hasKeyAndValue(reqJson, "goldId", "未包含金币ID");
    }

    @Override
    public void doCmd(ICmdDataFlowContext context, JSONObject reqJson) {
        GoldDetailDto goldDetailDto = BeanConvertUtil.covertBean(reqJson, GoldDetailDto.class);

        int count = goldDetailV1InnerServiceSMOImpl.queryGoldDetailsCount(goldDetailDto);

        List<GoldDetailDto> goldDetailDtos = null;

        if (count > 0) {
            goldDetailDtos = goldDetailV1InnerServiceSMOImpl.queryGoldDetails(goldDetailDto);
        } else {
            goldDetailDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) reqJson.getInteger("row")), count, goldDetailDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        context.setResponseEntity(responseEntity);
    }
}
